package com.xiaoye.qbproject.controller.qbzyk;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoye.qbproject.controller.base.BaseController;
import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.ZbRelation;
import com.xiaoye.qbproject.service.ZbRelationService;
import com.xiaoye.qbproject.vo.ZbRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description :
 * @create : 2024-12-28 14:38
 */
@RestController
@RequestMapping("/zbRelation")
public class ZbRelationController extends BaseController {
    @Autowired
    private ZbRelationService zbRelationService;

    /**
     * @param toId 上级节点
     * @description: 获取装备实体下, 指向自己的节点列表信息
     * @author: ChenYao
     * @return: java.lang.Appendable
     * @ceateDate: 2024-12-28 14:48
     */

    @GetMapping("/getFromList")
    public AjaxResult getFromZbRelation(@RequestParam("toId") String toId) {
        return AjaxResult.success(zbRelationService.getFromZbRelationList(toId));
    }

    /**
     * @param zbRelationVo
     * @description: 设置装备实体关系
     * @author: ChenYao
     * @return: com.xiaoye.qbproject.domain.AjaxResult
     * @ceateDate: 2024-12-28 15:01
     */

    @PostMapping()
    public AjaxResult addZbRelation(@RequestBody ZbRelationVo zbRelationVo) {
        zbRelationService.addZbRelation(zbRelationVo);
        return AjaxResult.success("实体关系设置完成");
    }

    @PutMapping()
    public AjaxResult editZbRelation(@RequestBody ZbRelationVo zbRelationVo) {
        zbRelationService.editZbRelation(zbRelationVo);
        return AjaxResult.success("实体关系更新完成");
    }

    @DeleteMapping()
    public AjaxResult deleteZbRelation(@RequestBody ZbRelationVo zbRelationVo) {
        List<String> fromIds = zbRelationVo.getFromIds();
        if (fromIds == null || fromIds.isEmpty()) {
            return AjaxResult.error("请选择要删除的实体关系");
        }
        zbRelationService.remove(Wrappers.<ZbRelation>lambdaQuery()
                .eq(ZbRelation::getToId, zbRelationVo.getToId())
                .in(ZbRelation::getFromId, fromIds)
        );
        return AjaxResult.success("实体关系删除完成");
    }

}
